package com.sxt.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.TreeNode;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.domain.Role;
import com.sxt.sys.mapper.RoleMapper;
import com.sxt.sys.service.PermissionService;
import com.sxt.sys.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sxt.sys.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2019-09-24
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

	@Autowired
	private PermissionService permissionService;

	@Override
	public boolean removeById(Serializable id) {
		//根据角色ID删除sys_role_permission
		this.getBaseMapper().deleteRolePermissionByRid(id);
		//根据角色ID删除sys_role_user
		this.getBaseMapper().deleteRoleUserByRid(id);
		return super.removeById(id);
	}

	/**
	 * 根据角色ID查询当前角色拥有的所有的权限或菜单ID
	 */
	@Override
	public List<Integer> queryRolePermissionIdsByRid(Integer roleId) {
		return this.getBaseMapper().queryRolePermissionIdsByRid(roleId);
	}

	/**
	 * 保存角色和菜单权限之间的关系
	 */
	@Override
	public void saveRolePermission(Integer rid, Integer[] ids) {
		RoleMapper roleMapper = this.getBaseMapper();
		//根据rid删除sys_role_permission
		roleMapper.deleteRolePermissionByRid(rid);
		if(ids!=null&&ids.length>0) {
			for (Integer pid : ids) {
				roleMapper.saveRolePermission(rid,pid);
			}
		}
	}

	/**
	 * 查询当前用户拥有的角色ID集合
	 */
	@Override
	public List<Integer> queryUserRoleIdsByUid(Integer id) {
		return this.getBaseMapper().queryUserRoleIdsByUid(id);
	}

//=====================================================================

	/**
	 * 查询
	 */
	public IPage<Role> loadAllRole(RoleVo roleVo) {
		IPage<Role> page=new Page<>(roleVo.getPage(), roleVo.getLimit());
		QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(roleVo.getName()), "name", roleVo.getName());
		queryWrapper.like(StringUtils.isNotBlank(roleVo.getRemark()), "remark", roleVo.getRemark());
		queryWrapper.eq(roleVo.getAvailable()!=null, "available", roleVo.getAvailable());
		queryWrapper.orderByDesc("createtime");
		this.page(page, queryWrapper);
		return page;
	}
	/**
	 * 添加
	 */
	public Boolean addRole(RoleVo roleVo) {
		try {
			roleVo.setCreatetime(new Date());
			this.save(roleVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 修改
	 */
	public Boolean updateRole(RoleVo roleVo) {
		try {
			this.updateById(roleVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 删除
	 */
	public Boolean deleteRole(Integer id) {
		try {
			this.removeById(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据角色ID加载菜单和权限的树的json串
	 */
	public List<TreeNode> initPermissionByRoleId(Integer roleId) {
		//查询所有可用的菜单和权限
		QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
		List<Permission> allPermissions = permissionService.list(queryWrapper);
		/**
		 * 1,根据角色ID查询当前角色拥有的所有的权限或菜单ID
		 * 2,根据查询出来的菜单ID查询权限和菜单数据
		 */
		List<Integer> currentRolePermissions=this.queryRolePermissionIdsByRid(roleId);
		List<Permission> carrentPermissions=null;
		if(currentRolePermissions.size()>0) { //如果有ID就去查询
			queryWrapper.in("id", currentRolePermissions);
			carrentPermissions = permissionService.list(queryWrapper);
		}else {
			carrentPermissions=new ArrayList<>();
		}
		//构造 List<TreeNode>
		List<TreeNode> nodes=new ArrayList<>();
		for (Permission p1 : allPermissions) {
			String checkArr="0";
			for (Permission p2 : carrentPermissions) {
				if(p1.getId()==p2.getId()) {
					checkArr="1";
					break;
				}
			}
			Boolean spread=(p1.getOpen()==null||p1.getOpen()==1)?true:false;
			nodes.add(new TreeNode(p1.getId(), p1.getPid(), p1.getTitle(), spread, checkArr));
		}
		return nodes;
	}


}
